using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using MacomberMap.Common.Components;
using MacomberMap.Common.Database_Interface;
using MacomberMap.Common.Internals;
using MacomberMap.Common.Types;

namespace MacomberMap.Common.Events
{
    /// <summary>
    /// This class encapsualtes the addition of an element into the database
    /// </summary>
    public class MM_Event_NewElement: MM_Event
    {
        #region Variable Declarations
        /// <summary>The element to be added</summary>
        public MM_Element NewElement;
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new element event
        /// </summary>
        /// <param name="NewElement">The element to be added</param>
        public MM_Event_NewElement(MM_Element NewElement)
        {
            this.NewElement = NewElement;
        }
        #endregion

        #region List view access
        /// <summary>
        /// Build a list view item 
        /// </summary>
        /// <returns></returns>
        public override ListViewItem BuildListViewItem()
        {
            ListViewItem NewItem = new ListViewItem(NewElement.ElemType.Name + " " + NewElement.Name);
            NewItem.SubItems.Add(this.EventState.ToString());
            NewItem.SubItems.Add(TimeText);
            NewItem.ImageIndex = EventType.Index;
            return NewItem;
        }

        /// <summary>
        /// Update the list view item for this element
        /// </summary>
        /// <param name="lvItem"></param>
        public override void UpdateListViewItem(ListViewItem lvItem)
        {
            lvItem.SubItems[0].Text = NewElement.ElemType.Name + " " + NewElement.Name;
            lvItem.SubItems[1].Text = EventState.ToString();
            lvItem.SubItems[2].Text = TimeText;
        }
        #endregion

        #region Details display
        /// <summary>
        /// Display details on an item
        /// </summary>
        /// <param name="OutParent">The pc</param>
        public override void DisplayDetails(Control OutParent)
        {
            TreeView tvOut = new TreeView();
            tvOut.Dock = DockStyle.Fill;
            this.AddNodeItem("Element", NewElement, tvOut.Nodes);
            tvOut.Nodes[0].EnsureVisible();
            OutParent.Controls.Add(tvOut);
        }
        #endregion

        /// <summary>
        /// Add menu items to the new element parameter
        /// </summary>
        /// <param name="OutMenu"></param>
        public override void AddMenuItems(ContextMenuStrip OutMenu)
        {
            (OutMenu.Items.Add("New: " + NewElement.ToString()) as ToolStripMenuItem).Enabled = false;
        }

        /// <summary>
        /// Approve the change to the system for this event
        /// </summary>
        /// <param name="ConnectedDatabases">The databases with which the event viewer is connected</param>
        public void ApproveChange(MM_Database[] ConnectedDatabases)
        {
       

            /*//Make the change internally
            if (MM_Repository.MismatchedTEIDs.ContainsKey(NewElement.TEID) && MM_Repository.MismatchedTEIDs[NewElement.TEID] == NewElement)
                MM_Repository.MismatchedTEIDs.Remove(NewElement.TEID);

            if (NewElement is MM_Zone == false)
                if (MM_Repository.TEIDs.ContainsKey(NewElement.TEID) && MM_Repository.TEIDs[NewElement.TEID] == NewElement)
                { }
                else if (!MM_Repository.TEIDs.ContainsKey(NewElement.TEID))
                    MM_Repository.TEIDs.Add(NewElement.TEID, NewElement);
            if (NewElement is MM_Substation && !MM_Repository.Substations.ContainsKey(NewElement.Name))                
                MM_Repository.Substations.Add(NewElement.Name, NewElement as MM_Substation);
            else if (NewElement is MM_Line)
                MM_Repository.Lines.Add(NewElement.Name, NewElement as MM_Line);
            else if (NewElement is MM_Company && !MM_Repository.Companies.ContainsKey(NewElement.Name))
                MM_Repository.Companies.Add(NewElement.Name, NewElement as MM_Company);
            else if (NewElement is MM_Boundary)
                if (NewElement.Name == "STATE")
                    MM_Repository.StateBoundary = NewElement as MM_Boundary;
                else
                    MM_Repository.Counties.Add(NewElement.Name, NewElement as MM_Boundary);
            */

            //Locate our Oracle database, and upload our lines
            foreach (MM_Database Db in ConnectedDatabases)
                if (Db is MM_Database_Oracle)
                    (Db as MM_Database_Oracle).AddOrUpdateElement(NewElement);
        }

        /// <summary>
        /// Compare the new event to another event
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public override int CompareTo(object obj)
        {
            return NewElement.CompareTo((obj as MM_Event_NewElement).NewElement);
        }
    }
}
